import static
from PyQt5 import QtCore, QtGui, QtWidgets

class CustomTreeWidget(QtWidgets.QTreeWidget):
    def paintEvent(self, event: QtGui.QPaintEvent):
        super().paintEvent(event)
        selected_items = self.selectedItems()
        if len(selected_items) > 0:
            r = self.visualItemRect(selected_items[0])
            painter = QtGui.QPainter(self.viewport())
            # 关键修改：保存和恢复QPainter状态
            painter.save()
            
            pen = QtGui.QPen(QtGui.QColor(0, 120, 215))
            pen.setWidth(0)
            painter.setPen(pen)
            painter.setRenderHint(QtGui.QPainter.Antialiasing)
            painter.setBrush(QtGui.QColor(0, 120, 215))
            line_width = 4
            rect = QtCore.QRectF(10, r.top() + 4.5, line_width, r.height() - 8)
            radius = line_width / 2
            painter.drawRoundedRect(rect, radius, radius)
            
            # 恢复QPainter状态，防止影响后续绘制
            painter.restore()

class UI_MainWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.menu_font = QtGui.QFont()
        self.menu_font.setPointSize(12)
        self.iconlist = []

        self.setupUi()
        self.show()

    def setupUi(self):
        self.setObjectName("MainWindow")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self)
        self.verticalLayout_2.setContentsMargins(0, 5, 0, 0)
        self.verticalLayout_2.setSpacing(0)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setSpacing(0)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setSpacing(0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.menu_collapse = QtWidgets.QTreeWidget(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.menu_collapse.sizePolicy().hasHeightForWidth())
        self.menu_collapse.setSizePolicy(sizePolicy)
        self.menu_collapse.setMaximumSize(QtCore.QSize(16777215, 25))
        self.menu_collapse.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.menu_collapse.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.menu_collapse.setAutoScrollMargin(16)
        self.menu_collapse.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.menu_collapse.setIndentation(0)
        self.menu_collapse.setRootIsDecorated(True)
        self.menu_collapse.setUniformRowHeights(False)
        self.menu_collapse.setAnimated(False)
        self.menu_collapse.setObjectName("menu_collapse")
        self.menu_collapse.headerItem().setTextAlignment(0, QtCore.Qt.AlignCenter)
        self.menu_collapse.headerItem().setTextAlignment(1, QtCore.Qt.AlignCenter)
        self.menu_collapse.setSortingEnabled(False)
        self.menu_collapse.header().setVisible(False)
        self.menu_collapse.header().setCascadingSectionResizes(True)
        self.menu_collapse.header().setDefaultSectionSize(20)
        self.menu_collapse.header().setHighlightSections(False)
        self.menu_collapse.header().setMinimumSectionSize(20)
        self.menu_collapse.header().setSortIndicatorShown(False)
        self.menu_collapse.header().setStretchLastSection(True)
        self.verticalLayout.addWidget(self.menu_collapse)
        self.menu_scroll = CustomTreeWidget(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.menu_scroll.sizePolicy().hasHeightForWidth())
        self.menu_scroll.setSizePolicy(sizePolicy)
        self.menu_scroll.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.menu_scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.menu_scroll.setAutoScrollMargin(16)
        self.menu_scroll.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.menu_scroll.setIndentation(0)
        self.menu_scroll.setRootIsDecorated(True)
        self.menu_scroll.setUniformRowHeights(False)
        self.menu_scroll.setAnimated(False)
        self.menu_scroll.setObjectName("menu_scroll")
        self.menu_scroll.headerItem().setTextAlignment(0, QtCore.Qt.AlignCenter)
        self.menu_scroll.headerItem().setTextAlignment(1, QtCore.Qt.AlignCenter)
        self.menu_scroll.header().setVisible(False)
        self.menu_scroll.header().setCascadingSectionResizes(True)
        self.menu_scroll.header().setDefaultSectionSize(20)
        self.menu_scroll.header().setHighlightSections(False)
        self.menu_scroll.header().setMinimumSectionSize(20)
        self.menu_scroll.header().setSortIndicatorShown(False)
        self.menu_scroll.header().setStretchLastSection(True)
        self.menu_scroll.setSortingEnabled(False)
        self.menu_scroll.setColumnCount(3)
        self.menu_scroll.setColumnWidth(0, 1)
        self.menu_scroll.setColumnWidth(1, 50)
        self.menu_scroll.setIconSize(QtCore.QSize(20, 20))
        self.menu_scroll.setFont(self.menu_font)
        self.verticalLayout.addWidget(self.menu_scroll)
        self.menu_bottom = CustomTreeWidget(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        self.menu_bottom.setSortingEnabled(False)
        sizePolicy.setHeightForWidth(self.menu_bottom.sizePolicy().hasHeightForWidth())
        self.menu_bottom.setSizePolicy(sizePolicy)
        self.menu_bottom.setMaximumSize(QtCore.QSize(16777215, 25))
        self.menu_bottom.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.menu_bottom.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.menu_bottom.setAutoScrollMargin(16)
        self.menu_bottom.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.menu_bottom.setIndentation(0)
        self.menu_bottom.setRootIsDecorated(True)
        self.menu_bottom.setUniformRowHeights(False)
        self.menu_bottom.setAnimated(False)
        self.menu_bottom.setObjectName("menu_bottom")
        self.menu_bottom.headerItem().setTextAlignment(0, QtCore.Qt.AlignCenter)
        self.menu_bottom.headerItem().setTextAlignment(1, QtCore.Qt.AlignCenter)
        self.menu_bottom.header().setVisible(False)
        self.menu_bottom.header().setCascadingSectionResizes(True)
        self.menu_bottom.header().setDefaultSectionSize(20)
        self.menu_bottom.header().setHighlightSections(False)
        self.menu_bottom.header().setMinimumSectionSize(20)
        self.menu_bottom.header().setSortIndicatorShown(False)
        self.menu_bottom.header().setStretchLastSection(True)
        self.verticalLayout.addWidget(self.menu_bottom)
        self.horizontalLayout.addLayout(self.verticalLayout)
        self.stackedWidget = QtWidgets.QStackedWidget(self)
        self.stackedWidget.setObjectName("stackedWidget")
        self.horizontalLayout.addWidget(self.stackedWidget)
        self.verticalLayout_2.addLayout(self.horizontalLayout)

    def addMenuCollapseWidget(self):
        self.menu_collapse.setColumnCount(2)
        self.menu_collapse.setColumnWidth(0, 1)
        self.menu_collapse.setColumnWidth(1, 50)
        self.menu_collapse.setIconSize(QtCore.QSize(20, 20))
        self.menu_collapse.setFont(self.menu_font)

        item = QtWidgets.QTreeWidgetItem(self.menu_collapse)
        item.setText(0, '')
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/menu/light/collapse.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        item.setIcon(1, icon)
        item.setFlags(QtCore.Qt.NoItemFlags|QtCore.Qt.ItemIsEnabled)
        self.iconlist.append((item, 'collapse.png'))
        self.menu_collapse.addTopLevelItem(item)

    def addMenuBottomWidget(self):
        self.menu_bottom.setColumnCount(3)
        self.menu_bottom.setColumnWidth(0, 1)
        self.menu_bottom.setColumnWidth(1, 50)
        self.menu_bottom.setIconSize(QtCore.QSize(20, 20))
        self.menu_bottom.setFont(self.menu_font)
        self.menu_bottom.headerItem().setTextAlignment(0, QtCore.Qt.AlignCenter)
        self.menu_bottom.headerItem().setTextAlignment(1, QtCore.Qt.AlignCenter)

        item = QtWidgets.QTreeWidgetItem(self.menu_bottom)
        item.setText(0, '')
        icon_setting = QtGui.QIcon()
        icon_setting.addPixmap(QtGui.QPixmap(":/menu/light/setting.png"),QtGui. QIcon.Normal, QtGui.QIcon.Off)
        item.setIcon(1, icon_setting)
        item.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEnabled)
        item.setText(2, "设置")
        self.iconlist.append((item, 'setting.png'))
        
        self.menu_bottom.addTopLevelItem(item)